<?
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}
include('/var/www/html/new/d/module/api/function.class.php');
include_once '/var/www/html/new/d/meituan/MT.class.php';
$meituan = new Meituan();
$url = 'http://lvyou.meituan.com/rhone/lv/order/consume/notice';  //美团消费地址
$txt=file_get_contents('php://input');
$meituan->logfile($txt,"ConsumeNotice");
$array = json_decode($txt,true);
$pft_order=  $array['Order16U'];//票付通订单号
$ordercall =  $array['OrderCall'];//远端订单号
$consume_num = $array['Tnumber']; //消费数量

$PFT_infunction = new AllFunction();
$pft_order_xml = $PFT_infunction->Select_PFTorder($pft_order);
$UUaids = (int)$pft_order_xml->Rec->UUaids; 
if($UUaids == 0){
	$UUsellid = (string)$pft_order_xml->Rec->UUsellid;
}
else $UUsellid = (string)$pft_order_xml->Rec->UUaid;

$sql = "select id,orderid,pre_num,done_num,rectime from pft_order_num_modify where orderid='$pft_order' order by id asc limit 1";
$GLOBALS['le']->query($sql);
if($GLOBALS['le']->fetch_assoc()){
	$nownum  = $GLOBALS['le']->f('done_num');
	 $totalnum =$GLOBALS['le']->f('pre_num');
}  
if(!$totalnum) $totalnum =(int)$pft_order_xml->Rec->UUtnum; 

$sql1 = "select supplierIdentity,signkey from uu_qunar_use  where DockingMode = 1 and fid =$UUsellid limit 1";
// echo $sql1;exit;
$GLOBALS['le']->query($sql1); 
$GLOBALS['le']->fetch_assoc();
$partnerId = $GLOBALS['le']->f('supplierIdentity');
$signkey = explode("|",$GLOBALS['le']->f('signkey'));
$clientID = $signkey[0];
$clientsecret = $signkey[1];

$body = array(
	'bookOrderId'=>$ordercall,
	'partnerOrderId'=>$pft_order,
	'orderStatus'=>8,
	'orderQuantity'=>$totalnum,
	'usedQuantity'=>$consume_num,
	'refundQuantity'=>$totalnum -$consume_num,	
);

date_default_timezone_set('GMT');
$date = date('D, d M Y H:i:s e', time());
date_default_timezone_set('PRC');
$authorization = buildSign('POST',$date,$clientID,$clientsecret);

$header = array(
	"Content-Type: application/json; charset=utf-8",
	"Date:" . $date,
	"PartnerId:" . $partnerId,
	"Authorization:" . $authorization,
);
$response= array(
	'code'=>200,
	'describe'=>'消费通知',
	'partnerId'=>$partnerId,
	'body'=>$body,
);
$data = json_encode($response);
$mt_response = mt_notice_post($data,$header,$url);
// $meituan->logfile($mt_response,"/var/www/html/new/d/meituan/logs/notice.txt");
$mt_response = json_decode($mt_response,true);
if($mt_response['code'] == 200) echo 200;
function mt_notice_post($data,$header,$url){ //美团提供的消费地址
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1); 
	curl_setopt($ch, CURLOPT_HEADER, 0); 
	curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL,$url); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
	$result=  curl_exec($ch); 
	curl_close($ch);
	return $result;
}

function buildSign($method,$date,$clientID,$clientsecret){
	$string_to_sign = $method . ' ' . '/rhone/lv/order/consume/notice' . "\n" . $date;
	$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $clientsecret, true));
	$authorization = 'MWS ' . $clientID . ':' . $signature;
	return $authorization;	
}

//$txt = '{"VerifyCode":"34fa98f1d8822da8bab8813e955492c0","Order16U":"1464325","ActionTime":"2015-07-12 11:30:39","OrderCall":"661199689_581996635","Tnumber":"1","OrderState":1}';

/*
{
  "code": 0,
  "describe": "string",
  "partnerId": 0,
  "body": {
    "bookOrderId": "string",
    "partnerOrderId": "string",
    "orderStatus": 0,
    "orderQuantity": 0,
    "usedQuantity": 0,
    "refundQuantity": 0
  }
}
*/
?>